<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BoxIntegralOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Support\JavaScript;
use Illuminate\Support\Str;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxIntegralOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BoxIntegralOrder(), function (Grid $grid) {
            $titles = ['id' => 'ID', 'username' => '名称', 'integralitme' => '标题' , 'integratcontent' => ' 详情' ,'city' => '城市' ,'address' => '地址'];
            $grid->export()->titles($titles)->disableExportAll()->filename('礼盒订单');
            $grid->disableCreateButton();
            $grid->column('id')->sortable();
            $grid->column('username')->display(function ($title) {
                $limit = 8; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($title, 'utf-8') > $limit) {
                    $title = mb_substr($title, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $title; // 返回关联表的  字段
            });

            $grid->column('integralitme')->display(function ($title) {
                $limit = 8; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($title, 'utf-8') > $limit) {
                    $title = mb_substr($title, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $title; // 返回关联表的  字段
            });
            $grid->column('integratcontent')->display(function ($title) {
                $limit = 8; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($title, 'utf-8') > $limit) {
                    $title = mb_substr($title, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $title; // 返回关联表的  字段
            });
            // $grid->column('')->image('http://laraveDact.com/uploads', 100, 100);
            $grid->column('integratimage')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, '/uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $grid->column('points');
            $grid->column('city');
            $grid->column('address');
            $grid->column('status')->using(['1' => '支付', '2' => '未支付', '3' => '已到货'])->badge([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default    
                1 => 'primary',
                2 => 'danger',
                3 => 'success',
            ]);
            $grid->column('date');
            
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    // protected function detail($id)
    // {
    //     return Show::make($id, new BoxIntegralOrder(), function (Show $show) {
    //         $show->field('id');
    //         $show->field('points');
    //         $show->field('status');
    //         $show->field('date');
    //         $show->field('integralitme');
    //         // $show->field('integratimage');
    //         $show->field('image')->image('http://laraveDact.com/uploads', 100, 100);
    //         $show->field('integratimage')->display(function ($avatar) {
    //             $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
    //             return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
    //         });
    //         $show->field('username');
    //         $show->field('address');
    //         $show->field('city');
            
    //     });
    // }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxIntegralOrder(), function (Form $form) {
            $form->display('id');
            $form->text('username')->disable();
            $form->number('points')->disable();
            $form->text('address')->disable();
            $form->text('city')->disable();
            $form->text('integralitme')->disable();
            $form->radio('status')->options([1 => '已支付', 2 => '未支付', 3 => '已完成'])->default();
            $form->image('integratimage')->disk('public')->disable()->accept('jpg,png,gif,jpeg')->maxSize(1024)->required()->autoUpload()->help('大小不要超过1024K');
            $form->markdown('integratcontent','商品详情')->options([
                'onpreviewing' => JavaScript::make(
                    <<<JS
            function() {
                // console.log("onpreviewing =>", this, this.id, this.settings);
                // on previewing you can custom css .editormd-preview-active
            }
            JS
                ),
            ])->disk('public')->disable();
            $form->date('date')->format('YYYY-MM-DD')->disable();
        });
    }
}
